// 获取App对象
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    homeList: [],
    show: false,
    placeNameList: [],
    activeId: 0,
    placeName: ''
  },

  // 选择所在地
  placeSlide(e) {
    let placeId = e.target.dataset.index;
    let placeNames = this.data.placeNameList[placeId];
    this.setData({
      activeId: placeId,
      placeName: placeNames
    })
    wx.setStorageSync('placeName', placeNames);
  },

  // 判断当前所在地
  placeNameJudge(arrList) {
    for (var i = 0; i < arrList.length; i++) {
      if(arrList[i]==this.data.placeName){
        this.setData({
          activeId:i
        })
      }
    }
  },

  showPopup() {
    this.setData({
      show: true
    });
  },

  onClose() {
    this.setData({
      show: false
    });
  },

  getData() {
    var _this = this;
    wx.request({
        url: 'https://www.fastmock.site/mock/942dfbb577a74c49a06b657043b1622a/lecake/lecakeHome',
        method: "GET",
        success(res) {
          _this.setData({
            homeList: res.data
          })
        }
      }),

      wx.request({
        url: 'https://www.fastmock.site/mock/942dfbb577a74c49a06b657043b1622a/lecake/generalData',
        method: "GET",
        success: (res) => {
          this.setData({
            placeNameList: res.data.placeNameList
          })
          this.placeNameJudge(this.data.placeNameList)
        }
      })
  },

  // 路由传参
  toDetail(goodsId) {
    wx.navigateTo({
      url: '../detail/detail?goodsId=' + goodsId.currentTarget.dataset.goodsid,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    if (app.globalData.placeName == '') {
      this.setData({
        placeName: '上海'
      })
    } else {
      this.setData({
        placeName: app.globalData.placeName
      })
    };
    this.getData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})